package com.app.kafka

import redis.clients.jedis.{Jedis, JedisPool}

object RedisUtil {
  //Redis服务器IP//Redis服务器IP

  private val URL = "10.11.12.13"
  //Redis的端口号
  private val PORT = 6379
  //可用连接实例的最大数目，默认值为8
  private val MAX_ACTIVE = 1024
  //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例，默认值也是8
  private val MAX_IDLE = 200
  //等待可用连接的最大时间，单位毫秒，默认值为-1，表示永不超时
  //如果超过等待时间，则直接抛出JedisConnectionException
  private val MAX_WAIT = 20000
  private val TIMEOUT = 10000
  //在borrow一个jedis实例时，是否提前进行validate操作,如果为true，则得到的jedis实例均是可用的
  private val TEST_ON_BORROW = true

  private var jedisPool:JedisPool =_


  {
    import redis.clients.jedis.JedisPoolConfig
    val config = new JedisPoolConfig
    config.setMaxIdle(MAX_ACTIVE)
    config.setMaxIdle(MAX_IDLE)
    config.setMaxWaitMillis(MAX_WAIT)
    config.setTestOnBorrow(TEST_ON_BORROW)
    this.jedisPool = new JedisPool(config, URL, PORT, TIMEOUT)
  }

  def getRedisInstance:Jedis={
    jedisPool.getResource
  }


  def closeRedis(): Unit ={
    jedisPool.close()
  }

}
